package algorithm.linear;

/**
 * 单链表逆转
 * @param <E>
 */
public class ReverseLinkedList<E> extends SinglyLinkList<E> {
    public ReverseLinkedList() {
        super();
    }

    public void reverse() {
        //p当前，f相当于p前面,q相当于p后面
        Node<E> p = this.head,q=null,front=null;
        while (p!=null){
            q = p.next;
            p.next = front;
            front = p;
            p = q;
        }
        this.head = front;
    }

    public static void main(String args[]) {
        ReverseLinkedList<Integer> list = new ReverseLinkedList<Integer>();
        for (int i=1;i<6;i++){
            list.add(0,new Integer(i));
        }
        System.out.println("单链表 "+list.toString());
        list.reverse();
        System.out.println("逆转后 "+ list.toString());
    }
}
